package cn.gok.service.impl;

import cn.gok.dao.NewTeacherDao;
import cn.gok.entity.Course;
import cn.gok.entity.Student;
import cn.gok.entity.Teacher;
import cn.gok.service.NewTeacherService;
import cn.gok.util.R;
import cn.gok.util.Sqlsessionutil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NewTeacherServiceImpl implements NewTeacherService {
    private SqlSession sqlSession;
    private NewTeacherDao newTeacherDao;

   public NewTeacherServiceImpl(){
        sqlSession = Sqlsessionutil.getSqlSessionFactory().openSession();
       newTeacherDao = sqlSession.getMapper(NewTeacherDao.class);
   }

    @Override
    public R login(String teaId, String password) {
        Teacher login = newTeacherDao.login(teaId, password);
        if (login!=null){
            R success = R.success1();
            success.put("teacher",login);
            return success;
        }
        return R.fail();
    }

    @Override
    public R checkPassword(String password, String teaId) {
        String s = newTeacherDao.checkPassword(teaId, password);
        if (s!=null){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R changePassword(String password, String teaId) {
        int i = newTeacherDao.changerPassword(password, teaId);
        sqlSession.commit();
        if (i>0){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R insertCourse(String couName, String startTime, String place, String max, String teaName, String teaId) {
        int i = newTeacherDao.insertCourse(couName, startTime, place, max, teaName, teaId);
        sqlSession.commit();
        if (i>0){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R selectCourse(String couName, String place, String state,String teaId,int currentPage, int pageSize) {
        List<Course> courses = newTeacherDao.selectCourse(couName, place, state,teaId);
        PageHelper.startPage(currentPage,pageSize);

        PageInfo pageInfo=new PageInfo(courses);
        R r=new R<>();
        r.put("result",true);
        r.put("msg","");

        Map<String ,Object> pagination=new HashMap<>();
        pagination.put("currentPage",currentPage);
        pagination.put("pageSize",pageSize);
        pagination.put("total",pageInfo.getTotal());

        r.put("pagination",pagination);
        r.put("datas",courses);

        return r;

    }

    @Override
    public R deleteCourse(String couId, String teaId) {
        int i = newTeacherDao.deleteCourse(couId, teaId);
        sqlSession.commit();
        if (i>0){
            return R.success();
        }
        return R.fail();
    }

    @Override
    public R selectStudent(String couId,int currentPage, int pageSize) {
        List<Student> students = newTeacherDao.selectStudent(couId);
        PageHelper.startPage(currentPage,pageSize);

        PageInfo pageInfo=new PageInfo(students);
        R r=new R<>();
        r.put("result",true);
        r.put("msg","");

        Map<String ,Object> pagination=new HashMap<>();
        pagination.put("currentPage",currentPage);
        pagination.put("pageSize",pageSize);
        pagination.put("total",pageInfo.getTotal());

        r.put("pagination",pagination);
        r.put("datas",students);

        return r;
    }

    @Override
    public R updateScores(String couId, String stu_id, String scores) {
        int i = newTeacherDao.updateScores(couId, stu_id, scores);
        sqlSession.commit();
        if (i>0){
            return R.success();
        }
        return R.fail();
    }

}
